using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float speed = 2f;  // 敌人移动速度
    public Transform player;  // 改为Transform
    private bool isFacingRight = true; 
    public UIManager uiManager;// 引用UIManager对象
    public GameObject deathAnimationPrefab;
    public int health=5;
    private Animator animator;
    void Start()
    {
        animator = GetComponent<Animator>();
        uiManager = FindObjectOfType<UIManager>();
        // 动态获取场景中的玩家对�?
        GameObject playerObject = GameObject.FindWithTag("Player");
        if (playerObject != null)
        {
            player = playerObject.transform;  // 获取玩家的Transform
        }
        else
        {
            Debug.LogError("Player object not found in the scene.");
        }
    }

    void Update()
    {
        // 确保玩家�? Transform 不为 null
        if (player != null)
        {
        // 计算敌人到玩家的方向
        Vector2 direction = (player.position - transform.position).normalized;

        // 移动敌人朝向玩家
        transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
        // 检查是否需要翻转敌�?
            if (direction.x > 0 && !isFacingRight)
            {
                Flip();
            }
            else if (direction.x < 0 && isFacingRight)
            {
                Flip();
            }
        }
        // 如果敌人超出屏幕范围，则销�?
        if (transform.position.y < -Camera.main.orthographicSize - 1)
        {
            Destroy(gameObject);
        }
    }

    // 翻转敌人的方�?
    void Flip()
    {
        isFacingRight = !isFacingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;  // 翻转X轴缩�?
        transform.localScale = theScale;
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Bullet"))
        {
            Debug.Log("碰撞检测！");
            Destroy(collision.gameObject);
            health--;
            if(health<=0){
            Destroy(gameObject);
            uiManager.AddScore(1);
            // 在敌人当前位置生成死亡动画预制体
            GameObject deathAnimation = Instantiate(deathAnimationPrefab, transform.position, Quaternion.identity);
            Animator animator = deathAnimation.GetComponent<Animator>();
            // 获取当前动画状态信息
            AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
            float animationLength = stateInfo.length;
            // 在动画播放完成后销毁预制体
            Destroy(deathAnimation, animationLength);
            }
            animator.SetTrigger("IsHurt");
        }
        else if (collision.CompareTag("Player"))
        {
            // 处理玩家被击中逻辑
            Debug.Log("碰撞检测到玩家�?");
            Destroy(gameObject);
            uiManager.TakeDamage(health);
            // 在敌人当前位置生成死亡动画预制体
            GameObject deathAnimation = Instantiate(deathAnimationPrefab, transform.position, Quaternion.identity);
            Animator animator = deathAnimation.GetComponent<Animator>();
            // 获取当前动画状态信息
            AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
            float animationLength = stateInfo.length;
            // 在动画播放完成后销毁预制体
            Destroy(deathAnimation, animationLength);
        }
    }
}


